<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use phpDocumentor\Reflection\Types\Self_;

/**
 * App\OrderRefund
 *
 * @property int $id
 * @property int $order_id 订单id
 * @property string $order_num 订单号
 * @property string $refund_num 退款单号
 * @property float $refund_amount 退款金额
 * @property int $status 0已记录申请，1退款成功，2退款失败
 * @property \Illuminate\Support\Carbon|null $deleted_at
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @method static bool|null forceDelete()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\OrderRefund newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\OrderRefund newQuery()
 * @method static \Illuminate\Database\Query\Builder|\App\OrderRefund onlyTrashed()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\OrderRefund query()
 * @method static bool|null restore()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\OrderRefund whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\OrderRefund whereDeletedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\OrderRefund whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\OrderRefund whereOrderId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\OrderRefund whereOrderNum($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\OrderRefund whereRefundAmount($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\OrderRefund whereRefundNum($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\OrderRefund whereStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\OrderRefund whereUpdatedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\OrderRefund withTrashed()
 * @method static \Illuminate\Database\Query\Builder|\App\OrderRefund withoutTrashed()
 * @mixin \Eloquent
 * @property string $fail_cause 退款单号
 * @method static \Illuminate\Database\Eloquent\Builder|\App\OrderRefund whereFailCause($value)
 * @property string $remark 退款备注
 * @method static \Illuminate\Database\Eloquent\Builder|\App\OrderRefund whereRemark($value)
 */
class OrderRefund extends Model
{
    protected $table = "order_refund";
    use SoftDeletes;

    public static function getOrderRefundAmount($order_id = 0){
        if(empty($order_id))
            return 0;

        $orderRefundAmount = self::whereOrderId($order_id)->whereStatus(1)->sum('refund_amount');

        return $orderRefundAmount;
    }

    public static function getOrderRefundInfo($order_id = 0){
        if(empty($order_id))
            return [];

        $data = [];
        /** @var OrderRefund $orderRefund */
        $orderRefundList = self::whereOrderId($order_id)->orderByDesc('id')->get();
        if(count($orderRefundList) > 0){
            foreach ($orderRefundList as $orderRefund) {
                $status = '处理中';
                if($orderRefund->status == 1){
                    $status = '退款成功';
                }elseif($orderRefund->status == 2){
                    $status = '退款失败';
                }

                $val = (object)[
                    'refund_num' => $orderRefund->refund_num,
                    'refund_reason' => $orderRefund->remark,
                    'refund_amount' => $orderRefund->refund_amount,
                    'status' => $status
                ];

                array_push($data,$val);
            }
        }

        return $data;
    }
}
